package week4.day26;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * @Author fengzihao
 * @Date 2024-05-10 20:25
 */
public class UDPChat {

}
class Sender{
    public static void main(String[] args) throws IOException {
        DatagramSocket senderDatagramSocket = new DatagramSocket();
        DatagramSocket receiverDatagramSocket = new DatagramSocket(9999);
        // 这个线程是用来发信息的
        new Thread(()->{
            DatagramPacket Sender = null;
            try {
                while(true){
                    Scanner scanner = new Scanner(System.in);
                    String inputMessage = scanner.nextLine();
                    byte[] bytes = inputMessage.getBytes();
                    Sender = new DatagramPacket(bytes,0,bytes.length, InetAddress.getByName("localhost"),8888);
                    senderDatagramSocket.send(Sender);
                    if ("exit".equals(inputMessage))
                        break;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                senderDatagramSocket.close();
            }
        }).start();
        // 这个线程是用来接受信息的
        new Thread(()->{
            try {
                    while (true){
                    byte[] bytes = new byte[8192];
                    DatagramPacket receiver = new DatagramPacket(bytes,0,bytes.length);
                    receiverDatagramSocket.receive(receiver);
                    byte[] receivers = receiver.getData();
                    int offset = receiver.getOffset();
                    int length = receiver.getLength();
                    String message = new String(receivers,offset,length);
                    System.out.println("对面说："+message);
                    if ("exit".equals(message))
                        break;
                    }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                receiverDatagramSocket.close();
            }
        }).start();
    }
}
class Receiver{
    public static void main(String[] args) throws IOException {
        DatagramSocket senderDatagramSocket = new DatagramSocket();
        DatagramSocket receiverDatagramSocket = new DatagramSocket(8888);
        // 这个线程是用来发信息的
        new Thread(()->{
            DatagramPacket Sender = null;
            try {
                while(true){
                    Scanner scanner = new Scanner(System.in);
                    String inputMessage = scanner.nextLine();
                    byte[] bytes = inputMessage.getBytes();
                    Sender = new DatagramPacket(bytes,0,bytes.length, InetAddress.getByName("192.168.8.9"),8888);
                    senderDatagramSocket.send(Sender);
                    if ("exit".equals(inputMessage))
                        break;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                senderDatagramSocket.close();
            }
        }).start();
        // 这个线程是用来接受信息的
        new Thread(()->{
            try {
                while (true){
                    byte[] bytes = new byte[8192];
                    DatagramPacket receiver = new DatagramPacket(bytes,0,bytes.length);
                    receiverDatagramSocket.receive(receiver);
                    byte[] receivers = receiver.getData();
                    int offset = receiver.getOffset();
                    int length = receiver.getLength();
                    String message = new String(receivers,offset,length);
                    System.out.println("对面说："+message);
                    if ("exit".equals(message))
                        break;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                receiverDatagramSocket.close();
            }
        }).start();
    }
}
